<?php
/**
 * Created by PhpStorm.
 * User: miidi
 * Date: 2018/7/16
 * Time: 9:58
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;
use think\facade\Request;
use think\Validate;

class BaseValidate extends Validate
{
  public function goCheck()
  {
    $request = Request::instance();
    $params = $request->param();
    $result = $this->check($params);
    if (!$result) {
      $exception = new ParameterException(
          [
            // $this->error有一个问题，并不是一定返回数组，需要判断
              'msg' => is_array($this->error) ? implode(
                  ';', $this->error) : $this->error,
          ]);
      throw $exception;

    } else {
      return true;
    }
  }

  protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
  {
    if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
      return true;
    }
    return $field . '必须是正整数';
  }
}